package com.chs.recoginition.controller;


import com.chs.recoginition.service.AndroidAppInfoService;
import com.chs.recoginition.vo.AndroidAppDownloadVo;
import com.chs.recoginition.vo.AndroidAppSaveVo;
import com.chs.recoginition.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * 安卓app 前端控制器
 * @author CHS
 * @since 2022-07-23
 */
@RestController
@Slf4j
@Validated
@RequestMapping("/api/android")
public class AndroidAppInfoController{

    @Resource
    private AndroidAppInfoService androidAppInfoService;




    /**
     * 上传文件
     *
     * @param response
     * @param saveVo
     * @return
     */
    @PostMapping("/upload")
    public Result uploadApp(HttpServletResponse response, AndroidAppSaveVo saveVo) throws IOException {
        androidAppInfoService.uploadApp(response, saveVo);
        return Result.success();
    }

    /**
     * 下载app
     *
     * @param response 响应
     * @param appName  app 名称
     * @param version  版本号
     * @throws FileNotFoundException
     */
    @GetMapping("/download")
    public void download(HttpServletResponse response, String appName, Integer version) throws FileNotFoundException {
        androidAppInfoService.downloadApp(response, appName, version);
    }

    /**
     * 下载最新的riddleApp
     * @param response
     * @throws FileNotFoundException
     */
    @GetMapping("/downloadRiddleApp")
    public void downloadRiddleApp(HttpServletResponse response) throws FileNotFoundException {
        String appName = "recoginition";
        AndroidAppDownloadVo result = androidAppInfoService.selectLastVersion(appName);
        androidAppInfoService.downloadApp(response, appName, result.getVersion());
    }

    /**
     * 查询最新版本的app 下载信息
     *
     * @param appName app 名称
     * @return 下载信息
     */
    @GetMapping("/update")
    public Result<AndroidAppDownloadVo> lastVersion(String appName) {
        AndroidAppDownloadVo result = androidAppInfoService.selectLastVersion(appName);
        return Result.success(result);
    }

}
